package com.hnjd.util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

public class UploadUtil {

	
	/**
	 * 返回文件名
	 * @param file
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	public static String upload(MultipartFile upload,HttpServletRequest request) throws IllegalStateException, IOException {
		//获取上传的位置
		String path = request.getSession().getServletContext().getRealPath("/upload/");
		System.out.println("上传路径为:"+path);
		File file = new File(path);
		System.out.println("file:"+file);
		//说明上传文件项
		//获取上传文件名称
		String fileName = upload.getOriginalFilename();
		//把文件的名称设置为唯一值
		String uuid = UUID.randomUUID().toString().replace("-", "");
		fileName = uuid+"_"+fileName;
		//向path目录传输文件
		upload.transferTo(new File(path,fileName));
		//copy一份文件
		String copyPaht = "/aimi/src/main/webapp/upload";
		upload.transferTo(new File(copyPaht,fileName));
		return "../upload/"+fileName;
	}
}
